﻿namespace Practices.JsonWebToken.Compatibility {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Script.Serialization;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class Encode : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            try {
                // 测试密钥
                string key = "OUe6jxKjYRLaEuxK6SklUjLU0MSfZK7+0Qf5hDVOAG4";
                // 测试令牌，真实环境中会以通过POST方式 由"用户平台"以"SPAppToken"参数传输。                
                string token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1bmlxdWVfbmFtZSI6InpoYW5nc2FuIn0.FHlK8QpuPmauyRr3TXBmHsTXECgaPg4y1B1BaTzmiKo";
                //string token = Request["SPAppToken"];
                // 解析令牌
                var principal = JsonWebToken.DecodeToObject(token, key) as IDictionary<string, object>;
                // 输出用户标识 （8位员工编号）
                Response.Write(principal["unique_name"]);
            } catch (Exception ex) {
                // 输出异常验证信息
                Response.Write(ex.Message);
            }
        }
    }
}